package com.bestcem.xm.award.grpc.client;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.award.util.DrawUtil;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.qdes.grpc.v1.services.*;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import zhongyan.xm.base.v1.Base;

import static com.bestcem.xm.award.constant.Constants.BLANK;

/**
 * 问卷项目grpc服务
 *
 * @author hang.li <hang.li@idiaoyan.com>
 * @version v1.0
 * @date 2021/12/10 17:28
 */
@Service
@Slf4j
public class AwardBaseQdesGrpcService {

    @GrpcClient(GrpcClientNames.DS_QDES)
    private BaseQdesServiceGrpc.BaseQdesServiceBlockingStub stub;

    /**
     * 获取单个project
     *
     * @param request 请求数据
     * @return 项目信息
     */
    public ProjectResponse getProject(GetProjectRequest request) {
        try {
            return stub.getProject(request);
        } catch (Exception e) {
            log.error("[award] base_qdes.proto/GetProject调用失败, request:{}; ErrorMsg: {}", request, e.getMessage(), e);
            return ProjectResponse.newBuilder().setStatus(error()).build();
        }
    }

    /**
     * 获取单个QStruct
     *
     * @param request 请求数据
     * @return 问卷结构数据
     */
    public QStructResponse getQStruct(GetQStructRequest request) {
        try {
            return stub.getQStruct(request);
        } catch (Exception e) {
            log.error("[award] base_qdes.proto/GetQStruct调用失败, request:{}; ErrorMsg: {}", request, e.getMessage(), e);
            return QStructResponse.newBuilder().setStatus(error()).build();
        }
    }

    /**
     * 中奖短信发送 获取问卷名称
     *
     * @param projectId 项目id
     * @param qStructId 问卷结构数据id
     * @return 问卷名称
     */
    public String getQdesName(String projectId, String qStructId) {
        String qdesName = BLANK;
        if (StringUtils.isBlank(qStructId)) {
            ProjectResponse projectResponse = getProject(GetProjectRequest.newBuilder().setId(projectId).build());
            if (Base.Code.OK_VALUE != projectResponse.getStatus().getCodeValue()) {
                log.error("[award] sendSms getProject,获取项目信息失败;projectId={}; ErrorMsg: {}", projectId, projectResponse.getStatus().getMessage());
                return BLANK;
            } else {
                try {
                    qStructId = projectResponse.getProject().getStructId();
                } catch (Exception e) {
                    log.error("[award] sms 获取问卷结果数据struct_id失败, projectId={}: ", projectId, e);
                    return BLANK;
                }
            }
        }
        if (StringUtils.isNotBlank(qStructId)) {
            QStructResponse qStructResponse = getQStruct(GetQStructRequest.newBuilder().setId(qStructId).build());
            if (Base.Code.OK_VALUE != qStructResponse.getStatus().getCodeValue()) {
                log.error("[award] sendSms getQStruct,获取问卷结构数据失败;projectId={} qStructId={}; ErrorMsg: {}", projectId, qStructId, qStructResponse.getStatus().getMessage());
                return BLANK;
            } else {
                // 问卷结构数据
                String data = null;
                try {
                    data = qStructResponse.getQStruct().getData();
                    log.info("[award] sendSms 问卷结构数据: data={}, projectId={}, qStructId={}", data, projectId, qStructId);
                    JSONObject jsonObject = JSONObject.parseObject(data);
                    qdesName = jsonObject.getJSONObject("desc_begin_page").getJSONObject("content").getString("text");
                    qdesName = DrawUtil.smsContentHtmlLabelsRegexp(qdesName);
                } catch (Exception e) {
                    log.error("[award] sendSms 解析问卷结构数据获取问卷名称失败: jsonData={}, projectId={}, qStructId={}", data, projectId, qStructId, e);
                    return BLANK;
                }
            }
        }
        return qdesName;
    }

    /**
     * 构造错误状态
     *
     * @return 错误信息
     */
    private Base.Status error() {
        Base.Status.Builder status = Base.Status.newBuilder();
        status.setMessage("baseQdes-grpc服务调用失败");
        status.setCode(Base.Code.ERROR);
        return status.build();
    }
}
